package cn.zhj.user.controller;

import cn.zhj.user.domain.User;
import cn.zhj.user.query.UserQuery;
import cn.zhj.user.service.IUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.zhj.util.JsonResult;

import java.util.List;
// Controller层
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    private IUserService iUserService;
    @GetMapping
    public List<User> findAll(){
        return iUserService.findAll();
    }
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id){
        return iUserService.findOne(id);
    }
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId()==null){
                iUserService.add(user);
            }else {
                iUserService.update(user);
            }
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iUserService.delete(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @PatchMapping
    public JsonResult patchDel(@RequestBody Long[] ids){
        try {
            iUserService.patchDel(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping
    public PageInfo<User> queryPage(@RequestBody UserQuery userQuery){
       return iUserService.queryPage(userQuery);
    }
}
